/**
 * 
 */
package net.iusr.jrules.eclipse.rules;

import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.tptp.platform.analysis.codereview.java.CodeReviewProvider;
import org.eclipse.tptp.platform.analysis.codereview.java.CodeReviewResource;
import org.eclipse.tptp.platform.analysis.core.history.AnalysisHistory;
import org.eclipse.tptp.platform.analysis.core.rule.AbstractAnalysisRule;

/**
 * 
 * @author <a href="mailto:iusr.jar@gmail.com">IUSR</a>
 * @since 2006-11-12 下午03:04:41
 * @version $Revision$
 */
public class AvoidRedundantMethodsRule extends AbstractAnalysisRule {

	/* (non-Javadoc)
	 * @see org.eclipse.tptp.platform.analysis.core.rule.AbstractAnalysisRule#analyze(org.eclipse.tptp.platform.analysis.core.history.AnalysisHistory)
	 */
	@Override
	public void analyze(AnalysisHistory history) {
		CodeReviewResource res = (CodeReviewResource) getProvider().getProperty(history.getHistoryId(), CodeReviewProvider.RESOURCE_PROPERTY);
		List<MethodDeclaration> methodList = getDeclaredMethods(res);
		Block methodBody = null;
		Statement st = null;
		for(MethodDeclaration md : methodList) {
			methodBody = md.getBody();
			if(methodBody.statements().size() == 1) {
				System.out.println("Found 1 statement method: " + md.getName());
				st = (Statement) methodBody.statements().get(0);
				List superMethods = res.getTypedNodeList(methodBody, ASTNode.SUPER_METHOD_INVOCATION);
				if(superMethods.size() == 1) {
					SuperMethodInvocation invoq = (SuperMethodInvocation) superMethods.get(0);
					System.out.println("Found 1 super method invocation: " + invoq.getName());
					List params = md.parameters();
					List arguments = invoq.arguments();
					System.out.println("params match? " + params.equals(arguments));
					if(Collections.disjoint(params, invoq.arguments())) {
						System.out.println("No params in common.");
					}
					// TODO: how to judge the invoked super method has the same signature of current method?
					if(params.equals(arguments)) {
						res.generateResultsForASTNode(this, history.getHistoryId(), md.getName());
					}
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	private List<MethodDeclaration> getDeclaredMethods(CodeReviewResource res) {
		return res.getTypedNodeList(res.getResourceCompUnit(), ASTNode.METHOD_DECLARATION);
	}
}
